package cn.edu.scau.daoImpl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import cn.edu.scau.dao.ArticleDao;
import cn.edu.scau.entity.Article;
/**
 * 文章接口的实例
 * @author Administrator
 *
 */
@Repository("articleDaoImpl")
public class ArticleDaoImpl extends BaseDaoImpl<Article> implements ArticleDao {
	/**
	 * 根据栏目的id展示
	 * @param columnId 栏目的id
	 * @param pageNo 从第几条开始
	 * @param pageSize 最多展示的条数
	 * @return
	 */
	@Override
	public List<Article> showArticleByColumnId(Integer columnId, Integer pageNo, Integer pageSize,boolean frontOrBackground) {
		
		Session session = getSessionFactory().openSession();
		if(frontOrBackground){
			String hql = "select ar from Article ar where ar.columnId = ?0 and ar.status = ?1";
			Query query = session.createQuery(hql).setParameter("0", columnId).setParameter("1", true);
			List<Article> list = query.setFirstResult((pageNo - 1) * pageSize).setMaxResults(pageSize).list();
			session.flush();
			session.close();
			return list;
		}else{
			String hql = "select ar from Article ar where ar.columnId = ?0";
			Query query = session.createQuery(hql).setParameter("0", columnId);
			List<Article> list = query.setFirstResult((pageNo - 1) * pageSize).setMaxResults(pageSize).list();
			session.flush();
			session.close();
			return list;
		}
		
	}
	/**
	 * 根据栏目id展示推荐内容，推荐内容为点赞次数前几
	 * @param columnId 栏目的id
	 * @param pageSize 点赞次数前几
	 * @return
	 */
	@Override
	public List<Article> showTheRecommendArticleByColumnId(Integer columnId, Integer pageSize) {
		
		Session session = getSessionFactory().openSession();
		String hql = "select ar from Article ar where ar.columnId = ?0 and ar.status = ?1 order by ar.likeTimes desc";
		Query query = session.createQuery(hql).setParameter("0", columnId).setParameter("1", true).setFirstResult(0).setMaxResults(pageSize);
		List<Article> list = query.list();
		session.flush();
		session.close();
		return list;
	}
	/**
	 * 根据栏目的id返回文章的总数量
	 * @param columnId 栏目的id
	 * @return frontOrBackground 判断前台展示还是后台展示
	 */
	@Override
	public long countArticleNumByColumnId(Integer columnId,boolean frontOrBackground) {
		Session session = getSessionFactory().openSession();
		if(frontOrBackground){
			String hql = "select count(*) from Article ar where ar.columnId = ?0 and ar.status = true";
			Query query = session.createQuery(hql).setParameter("0", columnId);
			List<?> list = query.list();
			session.flush();
			session.close();
			if(list != null && list.size() == 1){
				return (Long)list.get(0);
			}
			return 0;
		}else{
			String hql = "select count(*) from Article ar where ar.columnId = ?0";
			Query query = session.createQuery(hql).setParameter("0", columnId);
			List<?> list = query.list();
			session.flush();
			session.close();
			if(list != null && list.size() == 1){
				return (Long)list.get(0);
			}
			return 0;
		}
		
	}

}
